import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class InvalidCouponPage extends StatefulWidget {
  InvalidCouponPage({Key? key}) : super(key: key);

  @override
  State<InvalidCouponPage> createState() => _InvalidCouponPageState();
}

class _InvalidCouponPageState extends State<InvalidCouponPage> {
  RefreshController _refreshController = RefreshController();
  int subListNum = 5;
  List invalidCouponList = [
    {
      'name': '星轶上影影城(仙林九霄梦天地店)代金券',
      'price': '5',
      'validTime': '2022-03-03',
      'describe': '影院全场通用,暂不抵扣服务费',
      'rules': '无门槛',
      'isShowMerchantVoucher': '1',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '星轶上影影城(仙林九霄梦天地店)代金券',
      'price': '5',
      'validTime': '2022-02-24',
      'describe': '影院全场通用,暂不抵扣服务费',
      'rules': '无门槛',
      'isShowMerchantVoucher': '1',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '《不要忘记我爱你》买一赠一券',
      'price': '买一赠一',
      'validTime': '2022-02-11',
      'describe': '',
      'rules': '50元以内可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '《长津湖之水门桥》10元电影代金券',
      'price': '10',
      'validTime': '2022-02-02',
      'describe': '仅限《长津湖之水门桥》可用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '7元电影代金券',
      'price': '7',
      'validTime': '2019-10-02',
      'describe': '满35元可用,不限影片',
      'rules': '满35.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《奇迹》20元电影代金券',
      'price': '20',
      'validTime': '2021-02-18',
      'describe': '仅限《奇迹·笨小孩》可用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《四海》电影代金券',
      'price': '10',
      'validTime': '2022-02-01',
      'describe': '淘票票直连影院通用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '7元电影代金券',
      'price': '7',
      'validTime': '2022-01-31',
      'describe': '满35元可用,不限影片',
      'rules': '满35元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '星轶上影影城(仙林九霄梦天地店)代金券',
      'price': '3',
      'validTime': '2021-12-30',
      'describe': '去影院全场通用,暂不抵扣服务费',
      'rules': '无门槛',
      'isShowMerchantVoucher': '1',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '8元电影代金券',
      'price': '8',
      'validTime': '2021-12-22',
      'describe': '满35元可用,不限影片',
      'rules': '满35元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '8元电影代金券',
      'price': '8',
      'validTime': '2021-10-24',
      'describe': '满35元可用,不限影片',
      'rules': '满35元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '万达影城6元观影代金券(支付宝小程序)',
      'price': '6',
      'validTime': '2021-09-10',
      'describe': '仅限万达电影支付包小程序使用,满2张可用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '万达电影代金券',
      'price': '7',
      'validTime': '2021-05-27',
      'describe': '购买2张电影票使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': 'CGV电影代金券',
      'price': '7',
      'validTime': '2021-05-20',
      'describe': '购买2张电影票使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '3元电影代金券',
      'price': '3',
      'validTime': '2021-02-18',
      'describe': '购买2张电影票可用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票电影代金券',
      'price': '3',
      'validTime': '2020-10-21',
      'describe': '不限影片,全国通用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票电影代金券',
      'price': '5',
      'validTime': '2019-11-05',
      'describe': '仅限口碑app渠道可用|不可和活动叠加使用|票价满40元可用',
      'rules': '满40元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '金逸ShowBox影城(经开乐盈店)代金券',
      'price': '3',
      'validTime': '2019-11-03',
      'describe': '影院全场通用,暂不抵扣服务费',
      'rules': '无门槛',
      'isShowMerchantVoucher': '1',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '淘票票电影代金券',
      'price': '5',
      'validTime': '2019-10-12',
      'describe': '请在有效期内使用|不可和活动叠加使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '《中国机长》代金券',
      'price': '6',
      'validTime': '2019-10-10',
      'describe': '订单满2张电影票可用|不可和活动叠加使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《我和我的祖国》代金券',
      'price': '6',
      'validTime': '2019-10-10',
      'describe': '购买2张影票使用|不可和活动叠加使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '电影专享代金券',
      'price': '5',
      'validTime': '2019-10-10',
      'describe': '购票即可抵扣 快去购票|不可和活动叠加使用|票价满40元使用',
      'rules': '满40。0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《我和我的祖国》电影代金券',
      'price': '5',
      'validTime': '2019-10-02',
      'describe': '请在有效期内使用|不可和活动叠加使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票电影代金券',
      'price': '5',
      'validTime': '2019-10-02',
      'describe': '国庆打卡专享|不可和活动叠加使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《中国机长》5元代金券',
      'price': '5',
      'validTime': '2019-10-02',
      'describe': '请在有效期内使用|不可和活动叠加使用',
      'rules': '满2张可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '小目标-《哪吒之魔童降世》5元代金券',
      'price': '5',
      'validTime': '2019-08-21',
      'describe': '请在有效期内使用|不可和活动叠加使用|票价满35元使用',
      'rules': '满35.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票电影代金券',
      'price': '5',
      'validTime': '2019-08-21',
      'describe': '请在有效期内使用|不可和活动叠加使用|票价满35元使用',
      'rules': '满35.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《烈火英雄》5元电影代金券',
      'price': '5',
      'validTime': '2019-08-21',
      'describe': '请在有效期内使用|不可和活动叠加使用|票价满35元使用',
      'rules': '满35.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '《沉默的证人》5元电影代金券',
      'price': '5',
      'validTime': '2019-08-21',
      'describe': '请在有效期内使用|不可和活动叠加使用|票价满35元使用',
      'rules': '满35.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票代金券',
      'price': '5',
      'validTime': '2019-11-05',
      'describe': '淘票票指定影城通用|不可和活动叠加使用|票价满45元使用',
      'rules': '满45.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票电影代金券',
      'price': '5',
      'validTime': '2019-04-26',
      'describe': '淘票票直连影院可用|不可和活动叠加使用|票价满55元使用',
      'rules': '满55.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '淘票票电影红包',
      'price': '5',
      'validTime': '2018-11-11',
      'describe': '不可和活动叠加使用|不可和活动叠加使用|票价满35元使用',
      'rules': '满35.0元可用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '1',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
    {
      'name': '南昌星天地影城代金券',
      'price': '5',
      'validTime': '2018-08-15',
      'describe': '影院全场通用,暂不抵扣服务费',
      'rules': '无门槛',
      'isShowMerchantVoucher': '1',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '0',
    },
    {
      'name': '淘票票电影代金券',
      'price': '5',
      'validTime': '2018-06-24',
      'describe': '可与大盘活动叠加使用|票价满30元可用',
      'rules': '满30.0元使用',
      'isShowMerchantVoucher': '0',
      'superimposedWithPreferentialTickets': '0',
      'superimposedWithTheDisplayedSpecialPriceTicket': '1',
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('无效优惠券'),
        elevation: 0,
      ),
      backgroundColor: Color(0xFFf5f6f8),
      body: SmartRefresher(
        controller: _refreshController,
        enablePullDown: true,
        enablePullUp: true,
        onLoading: () async {
          await Future.delayed(Duration(milliseconds: 2000));
          if (subListNum < invalidCouponList.length) {
            if (mounted) setState(() => subListNum += 5);

            _refreshController.loadComplete();
          } else if (invalidCouponList.length - subListNum < 5) {
          } else {
            _refreshController.loadNoData();
          }
        },
        onRefresh: () async {
          await Future.delayed(Duration(milliseconds: 2000));
          if (mounted) setState(() => subListNum = 5);
          _refreshController.refreshCompleted();
        },
        child: SingleChildScrollView(
          child: Column(
            children: _invalidCouponItem(),
          ),
        ),
      ),
    );
  }

  _invalidCouponItem() {
    List<Widget> list = [];
    List tempList;

    invalidCouponList.length - subListNum < 5
        ? tempList = invalidCouponList
        : tempList = invalidCouponList.sublist(0, subListNum);
    tempList.forEach((element) {
      list.add(_invalidItem(
        element['name'],
        element['price'],
        element['validTime'],
        element['describe'],
        element['rules'],
        element['isShowMerchantVoucher'],
        element['superimposedWithPreferentialTickets'],
        element['superimposedWithTheDisplayedSpecialPriceTicket'],
      ));
    });
    return list;
  }

  _invalidItem(
      String name,
      String price,
      String validTime,
      String describe,
      String rules,
      String isShowMerchantVoucher,
      String isSuperimposedWithPreferentialTickets,
      String isSuperimposedWithTheDisplayedSpecialPriceTicket) {
    return Stack(
      children: [
        Container(
          margin: EdgeInsets.fromLTRB(10.w, 10.w, 10.w, 0),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10.r),
            color: Colors.white,
          ),
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Visibility(
                            visible: isShowMerchantVoucher == '1',
                            child: Padding(
                              padding: EdgeInsets.only(right: 5.w),
                              child: Container(
                                padding: EdgeInsets.symmetric(horizontal: 3.w),
                                decoration: BoxDecoration(
                                  color: Colors.green.withOpacity(0.6),
                                  borderRadius: BorderRadius.circular(3.r),
                                ),
                                child: Text(
                                  '商家券',
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 9.sp),
                                ),
                              ),
                            ),
                          ),
                          Text(
                            name,
                            style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black.withOpacity(0.6)),
                          )
                        ],
                      ),
                      SizedBox(height: 5.h),
                      Text(
                        validTime,
                        style: TextStyle(
                          fontSize: 11.5.sp,
                          color: Colors.black.withOpacity(0.3),
                        ),
                      ),
                      SizedBox(height: 5.h),
                      Row(
                        children: [
                          Visibility(
                            visible:
                                isSuperimposedWithPreferentialTickets == '1',
                            child: _smallWidget('不与特惠票叠加'),
                          ),
                          SizedBox(width: 3.w),
                          Visibility(
                            visible:
                                isSuperimposedWithTheDisplayedSpecialPriceTicket ==
                                    '1',
                            child: _smallWidget('不与限时特价叠加'),
                          )
                        ],
                      )

                      // SizedBox(height: 15.h),
                    ],
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      price.length > 2
                          ? Text(
                              price,
                              style: TextStyle(
                                  fontSize: 14.sp,
                                  color: Colors.red.withOpacity(0.7)),
                            )
                          : Row(
                              children: [
                                Text(
                                  '￥',
                                  style: TextStyle(
                                      fontSize: 8.sp,
                                      color: Colors.red.withOpacity(0.8)),
                                ),
                                Text(
                                  price,
                                  style: TextStyle(
                                      fontSize: 18.sp,
                                      color: Colors.red.withOpacity(0.7)),
                                ),
                              ],
                            ),
                      Text(
                        rules,
                        style: TextStyle(
                            fontSize: 9.5.sp,
                            color: Colors.black.withOpacity(0.3)),
                      )
                    ],
                  )
                ],
              ),
              Divider(
                thickness: 1,
              ),
              Text(
                describe,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    fontSize: 12.sp, color: Colors.black.withOpacity(0.3)),
              )
            ],
          ),
        ),
        Positioned(
          // right: 10,
          right: 15.w,
          bottom: -10.w,
          child: Container(
            alignment: Alignment.center,
            width: 36.w,
            height: 36.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(18.r),
              border: Border.all(
                color: Colors.black.withOpacity(0.1),
              ),
            ),
            child: Text(
              '已过期',
              style: TextStyle(
                  color: Colors.black.withOpacity(0.1), fontSize: 10.sp),
            ),
          ),
        )
      ],
    );
  }

  Widget _smallWidget(String value) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 1.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(3.r),
        border: Border.all(
          color: Colors.orange.withOpacity(0.3),
        ),
      ),
      child: Text(
        value,
        style: TextStyle(color: Colors.orange.withOpacity(0.9), fontSize: 8.sp),
      ),
    );
  }
}
